package com.dlb.andorid.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.App;
import com.dlb.andorid.ProductAdapter;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.bean.MyAddress;
import com.dlb.andorid.bean.ProductBean;
import com.dlb.andorid.bean.ServerResult;
import com.dlb.andorid.bean.SuperMarket;
import com.dlb.andorid.bean.SuperMarketType;
import com.dlb.andorid.presenter.SuperMarketPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class SuperMarketActivity extends BaseCommonActivity implements XContract.ISuperMarketView, OnLoadMoreListener, OnRefreshListener {

	SuperMarketPresenter mPresenter;

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new SuperMarketPresenter(this);
		return mPresenter;
	}

	@Override
	protected void initData() {

	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_super_market;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		return "便民超市";
	}


	TextView tv_name, tv_addr;
	private View ll_address;
	RecyclerView rv_type, rv_home;
	BaseQuickAdapter<SuperMarketType, BaseViewHolder> mTypeAdapter;
	BaseQuickAdapter<ProductBean, BaseViewHolder> mHomeAdapter;


	List<SuperMarketType> mTypes = new ArrayList<>();
	List<ProductBean> mProducts = new ArrayList<>();

	int mCurrentSelectTypePosition = 0;

	protected RefreshLayout refreshLayout;


	@Override
	protected void initViews() {
		super.initViews();


		tv_name = findViewById(R.id.tv_name);
		tv_addr = findViewById(R.id.tv_addr);

		ll_address = findViewById(R.id.ll_address);
		ll_address.setOnClickListener(this);

		refreshLayout = findViewById(R.id.refreshLayout);
		refreshLayout.setOnLoadMoreListener(this);
		refreshLayout.setOnRefreshListener(this);


		rv_type = findViewById(R.id.rv_type);
		mTypeAdapter = new BaseQuickAdapter<SuperMarketType, BaseViewHolder>(R.layout.list_item_home_type, mTypes) {
			@Override
			protected void convert(BaseViewHolder helper, SuperMarketType item) {

				if (mCurrentSelectTypePosition == helper.getLayoutPosition()) {
					helper.setBackgroundRes(R.id.tv_name, R.drawable.shape_home_type_selected);
					helper.setTextColor(R.id.tv_name, Color.WHITE);
				} else {
					helper.setBackgroundRes(R.id.tv_name, R.drawable.shape_home_type_normal);
					helper.setTextColor(R.id.tv_name, getResources().getColor(R.color.colorPrimary));
				}

				helper.setText(R.id.tv_name, item.getSupermarket_commodity_name());
			}
		};

		mTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				mCurrentSelectTypePosition = position;
				page = 1;
				isLoadMore = false;
				mPresenter.getSupermarketProduct(mSuperMarket.getSupermarket_id(), mTypes.get(position).getSupermarket_commodity_type(), page);
				mTypeAdapter.notifyDataSetChanged();
			}
		});

		rv_type.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
		rv_type.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		rv_type.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		rv_type.setHasFixedSize(true);
		//解决更新 闪烁
		rv_type.setItemAnimator(null);
		rv_type.setNestedScrollingEnabled(false);
		rv_type.setAdapter(mTypeAdapter);

		rv_home = findViewById(R.id.rv_home);

		mHomeAdapter = new ProductAdapter(mProducts);

		rv_home.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
		rv_home.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		rv_home.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		rv_home.setLayerType(View.LAYER_TYPE_NONE, null);
		rv_home.setHasFixedSize(true);
		//解决更新 闪烁
		rv_home.setItemAnimator(null);
		rv_home.setNestedScrollingEnabled(false);
		rv_home.setAdapter(mHomeAdapter);

		mHomeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				Bundle bundle = new Bundle();
				bundle.putLong("id", mProducts.get(position).getId());
				go2Activity(ProductDetailActivity.class, bundle, false);
			}
		});

		mHomeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
			@Override
			public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
				//toastMsg(""+mProducts.get(position).getId());
				Log.e("kwan", "car:" + mProducts.get(position).getId());
				if (view.getId() == R.id.iv_car) {
					Log.e("kwan", "car:" + mProducts.get(position).getId());
					showProgress("");
					mPresenter.addCar(mProducts.get(position).getId(), App.mUserInfo.getUid(), 1, mProducts.get(position).getType_id(), mProducts.get(position).getSale_price());
				}
			}
		});

	}

	@Override
	public String getPageName() {
		return null;
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == ll_address) {
			go2ActivityForResult(AddressListActivity.class, 100, null, 0, 0);
		}
	}


	long address_id = 0;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.e("kwan", "onActivityResult");
		if (requestCode == 100) {//选择地址返回
			MyAddress mMyAddress = (MyAddress) data.getBundleExtra("data").getSerializable("address");
			tv_name.setText(mMyAddress.getName() + " " + mMyAddress.getPhone());
			tv_addr.setText(mMyAddress.getProvince() + mMyAddress.getCity() + mMyAddress.getCity() + " " + mMyAddress.getAreainfo());
			address_id = mMyAddress.getAddress_id();
			mPresenter.getSupermarket(address_id);
		}
	}

	SuperMarket mSuperMarket;

	@Override
	public void getSupermarketSuccess(SuperMarket data) {
		mSuperMarket = data;

		if (mSuperMarket != null) {
			mPresenter.getSupermarketType(mSuperMarket.getSupermarket_id());
			mImageLoader.loadUrl(mSuperMarket.getSupermarket_pic(), (ImageView) findViewById(R.id.iv_icon));
		}
	}


	@Override
	public void getSupermarketTypeSuccess(List<SuperMarketType> data) {

		mTypes.clear();
		mTypes.addAll(data);
		mTypeAdapter.notifyDataSetChanged();
		mPresenter.getSupermarketProduct(mSuperMarket.getSupermarket_id(), mTypes.get(0).getSupermarket_commodity_type(), 1);

	}

	@Override
	public void getSupermarketProductSuccess(List<ProductBean> data) {
		refreshLayout.finishRefresh();
		refreshLayout.finishLoadMore();

		if (isLoadMore) {

			if (data == null)
				return;

			mProducts.addAll(data);
			mHomeAdapter.setNewData(mProducts);
		} else {
			mProducts.clear();
			if (data != null)
				mProducts.addAll(data);

			mHomeAdapter.setNewData(mProducts);
		}
	}

	int page = 1;
	boolean isLoadMore;

	@Override
	public void onRefresh(@NonNull RefreshLayout refreshLayout) {

		if (mSuperMarket != null) {


			page = 1;
			isLoadMore = false;
			mPresenter.getSupermarketProduct(mSuperMarket.getSupermarket_id(), mTypes.get(mCurrentSelectTypePosition).getSupermarket_commodity_type(), page);

		} else {
			refreshLayout.finishRefresh();
		}
	}

	@Override
	public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

		if (mSuperMarket != null) {

			isLoadMore = true;
			page++;
			mPresenter.getSupermarketProduct(mSuperMarket.getSupermarket_id(), mTypes.get(mCurrentSelectTypePosition).getSupermarket_commodity_type(), page);
		} else {
			refreshLayout.finishLoadMore();
		}
	}

	@Override
	public void addCarSuccess(ServerResult data) {
		dismissProgress();
		toastMsg(data.getMsg());
	}
}
